/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.athena.objects.Game;
import com.athena.objects.Player;
import com.smartfoxserver.v2.annotations.Instantiation;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.exceptions.SFSRuntimeException;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author dodinhchien
 */
@Instantiation(Instantiation.InstantiationMode.SINGLE_INSTANCE)
public class MoveHandle extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {

        if (!params.containsKey("x")) {
            throw new SFSRuntimeException("Invalid request, one mandatory param is missing. Required 'x' and 'y'");
        }
        synchronized (this) {
            FourinarowExtension extension = (FourinarowExtension) getParentExtension();
            Player player = extension.getUsersList().get(user.getId());
            if (player.getRoomId() != -1) {
                Game game = extension.getGamesList().get(player.getRoomId());
                try {
                    if(game != null)
                        game.moveHandle(extension, player, params);
                } catch (SQLException ex) {
                    trace(ExtensionLogLevel.ERROR, "sql ploblem", ex.toString());
                }
            }
        }
    }

}
